﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Net;
using DTO;
using Models;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
public static class Constant
{
    public static string ROOT_PATH = ConfigurationManager.AppSettings["ROOT_PATH"];
    public static string OWNER = ConfigurationManager.AppSettings["Owner"];
    public static string loginAdmin = ConfigurationManager.AppSettings["loginAdmin"];
    public static string strSiteTitle = ConfigurationManager.AppSettings["SiteTitle"];
    public static string email = ConfigurationManager.AppSettings["email"];//TimeCache//LenghtTitle
    public static int lenghtTitle = 90;
    public static List<Module> GetModule()
    {
        List<Module> list = new List<Module>();
        Module module = new Module();
        module.Id = 1;
        module.ModuleTitle = "Hệ Thống";
        module.ModuleName = "ht";
        module.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(module);
        
        module = new Module();
        module.Id = 2;
        module.ModuleTitle = "Quản Trị";
        module.ModuleName = "qt";
        module.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(module);
        /*
        module = new Module();
        module.Id = 3;
        module.ModuleTitle = "Công Cụ";
        module.ModuleName = "cc";
        module.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(module);
        */
        module = new Module();
        module.Id = 4;
        module.ModuleTitle = "Báo Cáo";
        module.ModuleName = "bc";
        module.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(module);
        module = new Module();
        module.Id = 5;
        module.ModuleTitle = "Trợ Giúp";
        module.ModuleName = "tg";
        module.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(module);
        return list;
    }

    public static List<Item> GetItems() {
        List<Item> list = new List<Item>();
        Item _item;
        //he thong
        _item = new Item();
        _item.Id = 1;
        _item.ModuleId = 1;
        _item.ItemTitle = "Thay Đổi Mật Khẩu";
        _item.ItemName = "cp";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        _item = new Item();
        _item.Id = 2;
        _item.ModuleId = 1;
        _item.ItemTitle = "Nhóm Người Dùng";
        _item.ItemName = "nnd";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        _item = new Item();
        _item.Id = 3;
        _item.ModuleId = 1;
        _item.ItemTitle = "Người Dùng";
        _item.ItemName = "nd";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);

        //quản trị hệ thống
        _item = new Item();
        _item.Id = 4;
        _item.ModuleId = 2;
        _item.ItemTitle = "Tuyến Phát";
        _item.ItemName = "tphat";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        _item = new Item();
        _item.Id = 5;
        _item.ModuleId = 2;
        _item.ItemTitle = "Đơn Vị";
        _item.ItemName = "donvi";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);

        _item = new Item();
        _item.Id = 5;
        _item.ModuleId = 2;
        _item.ItemTitle = "Khách Hàng";
        _item.ItemName = "kh";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        _item = new Item();
        _item.Id = 6;
        _item.ModuleId = 2;
        _item.ItemTitle = "Loại Bưu Phẩm Bưu Kiện";
        _item.ItemName = "lbpbk";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        _item = new Item();
        _item.Id = 7;
        _item.ModuleId = 2;
        _item.ItemTitle = "Bưu Phẩm Bưu Kiện";
        _item.ItemName = "bpbk";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        _item = new Item();
        _item.Id = 8;
        _item.ModuleId = 2;
        _item.ItemTitle = "Trạng Thái Bill";
        _item.ItemName = "ttbill";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);
        
        _item = new Item();
        _item.Id = 10;
        _item.ModuleId = 2;
        _item.ItemTitle = "Bảng Giá";
        _item.ItemName = "bg";
        _item.Url = ROOT_PATH + "admin/List.aspx";
        list.Add(_item);


        ///phan bao cao
        ///
        _item = new Item();
        _item.Id = 11;
        _item.ModuleId = 4;
        _item.ItemTitle = "Báo Cáo Doanh Thu";
        _item.ItemName = "bpbk";
        _item.Url = ROOT_PATH + "admin/Report.aspx";
        list.Add(_item);

        return list;
    }

    public static List<Item> GetItemsInModule(int moduleId) {
        List<Item> list = new List<Item>();
        List<Item> items = GetItems();
        foreach (Item _item in items) {
            if (_item.ModuleId == moduleId) {
                list.Add(_item);
            }
        }
        return list;
    }

    public static List<String> GetCssAdmin()
    {                
        List<String> list = new List<string>();
        list.Add("<link rel=\"stylesheet\" type=\"text/css\" href=\""  + "css/style.css\" />");
        list.Add("<link rel=\"stylesheet\" type=\"text/css\" href=\""  +  "css/niceforms-default.css\" />");
        return list;
    }

    public static void ExportToPDF(GridView grid, string fileName)
    {
        HttpResponse Response = System.Web.HttpContext.Current.Response;
        grid.AllowPaging = false;
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=" + fileName);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        grid.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }

    public static void ExportToExcel(GridView grid, string fileName)
    {
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
        grid.RenderControl(htmlWrite);
        System.IO.StreamWriter vw = new System.IO.StreamWriter(fileName, true);
        stringWriter.ToString().Normalize();
        vw.Write(stringWriter.ToString());
        vw.Flush();
        vw.Close();
        WriteAttachment(fileName, "application/vnd.ms-excel", stringWriter.ToString());
    }

    public static void WriteAttachment(string FileName, string FileType, string content)
    {
        HttpResponse Response = System.Web.HttpContext.Current.Response;
        Response.ClearHeaders();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = FileType;
        Response.Write(content);
        Response.End();
    }


}